package org.rcy.framework.mq.listener;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.config.BeanPostProcessor;

public class RabbitMessageListenerRegistrarPostProcessor implements BeanPostProcessor, BeanFactoryAware {
    private BeanFactory beanFactory;

    public RabbitMessageListenerRegistrarPostProcessor() {
    }

    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        RabbitMessageListenerRegistrar provider = (RabbitMessageListenerRegistrar)this.beanFactory.getBean(RabbitMessageListenerRegistrar.class);
        if (provider == null) {
            throw new NullPointerException("MessageListenerProvider should not be null");
        } else {
            if (bean instanceof RabbitMessageListener) {
                RabbitMessageListener<?> listener = (RabbitMessageListener)bean;
                provider.register(listener);
            }

            return bean;
        }
    }

    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        this.beanFactory = beanFactory;
    }
}
